package com.fhz.day011;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.provider.Telephony;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ListView lv;

    private List<Phone> list;
    private boolean isPer = false;
    private static final String TAG = "feng";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        list = new ArrayList<>();

//        lv.setAdapter();
        //6.0以上.动态权限
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
                requestPermissions(new String[]{
                        Manifest.permission.READ_CONTACTS,
                        Manifest.permission.WRITE_CONTACTS,
                        Manifest.permission.READ_SMS,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE
                },110);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == 110 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            isPer = true;
        }else{
            finish();
        }
    }

    public void contacts(View view) {
        if(isPer){
            //写到这里
            ContentResolver contentResolver = getContentResolver();
            String[] strings = new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER};
            Cursor query = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, strings, null, null, null);
            while (query.moveToNext()) {
                int columnIndex = query.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                String name = query.getString(columnIndex);
                int columnIndex1 = query.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                String nub = query.getString(columnIndex1);
                //
                Phone phone = new Phone(name, nub);
                list.add(phone);
                Log.i(TAG, "onRequestPermissionsResult: " + name);
            }
            query.close();
        }

    }

    public void sms(View view) {
        if(isPer){
            ContentResolver contentResolver = getContentResolver();
            String[] strings = new String[]{Telephony.Sms.ADDRESS, Telephony.Sms.BODY};
            Cursor query = contentResolver.query(Telephony.Sms.CONTENT_URI, strings, null, null, null);
            while (query.moveToNext()){
                int columnIndex = query.getColumnIndex(Telephony.Sms.ADDRESS);
                String address = query.getString(columnIndex);
                int columnIndex1 = query.getColumnIndex(Telephony.Sms.BODY);
                String body = query.getString(columnIndex1);
                Log.i(TAG, "sms: "+address+":"+body);
            }
            query.close();
        }
    }

    public void pic(View view) {
        if(isPer){
            ContentResolver contentResolver = getContentResolver();
            String[] strings = new String[]{MediaStore.Images.Media.TITLE,
                    MediaStore.Images.Media.SIZE,
                    MediaStore.Images.Media.DATA,
                    MediaStore.Images.Media.HEIGHT,
                    MediaStore.Images.Media.WIDTH,
            };
            Cursor query = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, strings, null, null, null);
            while (query.moveToNext()){
                int columnIndex = query.getColumnIndex(MediaStore.Images.Media.TITLE);
                String title = query.getString(columnIndex);
                int columnIndex1 = query.getColumnIndex(MediaStore.Images.Media.DATA);
                String data = query.getString(columnIndex1);
                Log.i(TAG, "pic: "+title+":"+data);
            }
            query.close();
        }

    }

    public void ved(View view) {
        if(isPer){
            ContentResolver contentResolver = getContentResolver();
            String[] strings = new String[]{
                    MediaStore.Video.Media.DISPLAY_NAME,
                    MediaStore.Video.Media.DATA
            };
            Cursor query = contentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, strings, null, null, null);
            while (query.moveToNext()){
                int columnIndex = query.getColumnIndex(MediaStore.Video.Media.DISPLAY_NAME);
                String title = query.getString(columnIndex);
                int columnIndex1 = query.getColumnIndex(MediaStore.Video.Media.DATA);
                String data = query.getString(columnIndex1);
                Log.i(TAG, "video: "+title+":"+data);
            }
            query.close();
        }
    }

    public void aud(View view) {
        if(isPer){
            ContentResolver contentResolver = getContentResolver();
            String[] strings = new String[]{
                    MediaStore.Audio.Media.ARTIST,
                    MediaStore.Audio.Media.DATA
            };
            Cursor query = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, strings, null, null, null);
            while (query.moveToNext()){
                int columnIndex = query.getColumnIndex(MediaStore.Audio.Media.ARTIST);
                String title = query.getString(columnIndex);
                int columnIndex1 = query.getColumnIndex(MediaStore.Audio.Media.DATA);
                String data = query.getString(columnIndex1);
                Log.i(TAG, "Audio: "+title+":"+data);
            }
            query.close();
        }
    }
}